﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Products : System.Web.UI.UserControl
{
    public static string connString = "user id=portaluser;data source=172.25.10.8;initial catalog=VoiceNet_Focus;password=sqluser";

    public SqlConnection conn = new SqlConnection(connString);

    private string ordType = "3";


    protected void Page_Load(object sender, System.EventArgs e)
    {
        //Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "confirmdeletekey", ResolveClientUrl("~/scripts/ConfirmDelete.js"));

        if (!IsPostBack)
        {
            //btnClear.Attributes.Add("confirmdeletekey", "return confirm('Are you sure you want to delete all products for this order?');");
            //btnClear.Attributes["confirmdeletekey"];

            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "confirmdeletekey", "function confirm_delete() { return confirm('Are you sure you want to delete the all products for this order?'); }", true);
            //Page.ClientScript.RegisterClientScriptInclude("confirmdeletekey", "ConfirmDelete.js");



            //btnClear.Attributes["confirmdeletekey"]


            loadProductTypes();

            loadOrders();

        }


    }

    public void SetOrderType(string orderType)
    {
        ordType = orderType;
    }

    public void loadProductTypes()
    {
        SqlDataAdapter ProductTypesDA = default(SqlDataAdapter);
        DataSet ProductTypesDS = default(DataSet);

        connString = "user id=portalUser;data source=" + Session["Servername"] + ";initial catalog=VoiceNet_Focus;password=sqluser";
        conn.ConnectionString = connString;

        System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
        string lccommand = null;

        lccommand = "Select distinct wpt.producttypeid, wpt.producttype from webproducts wp (nolock) left outer join webproducttypes wpt (nolock) on wp.producttypeid = wpt.producttypeid where wp.active = 1 and wpt.active = 1 and wp.producttypeid = " + ordType + " and wp.campaignname = '" + Session["campaignname"] + "' order by wpt.producttypeid ";

        ProductTypesDA = new SqlDataAdapter(lccommand, conn);
        ProductTypesDS = new DataSet();
        ProductTypesDA.Fill(ProductTypesDS, "AllTables");
        //ddlProductTypes.DataSource = ProductTypesDS;
        ddlProductTypes.DataSource = ProductTypesDS.Tables[0];
        ddlProductTypes.DataTextField = ProductTypesDS.Tables[0].Columns["producttype"].ColumnName.ToString();
        ddlProductTypes.DataValueField = ProductTypesDS.Tables[0].Columns["producttypeid"].ColumnName.ToString();
        ddlProductTypes.DataBind();
        ddlProductTypes.Items.Insert(0, new ListItem("", ""));
        //ddlProductTypes.SelectedIndex = 0;
        lblError1.Text = "";
    }

    private void loadProducts()
    {
        lbProducts.Items.Clear();

        DataTable dtProd = new DataTable();
        dtProd = ((DataAccess)Session["App"]).GetProducts();

        foreach (DataRow dr in dtProd.Rows)
        {
            if (dr["producttypeid"].ToString() == ddlProductTypes.SelectedValue)
            {
                if (dr["group2"].ToString() == ddlAutoship.SelectedValue)
                {
                    lbProducts.Items.Add(new ListItem(dr["productdesc"].ToString(), dr["productid"].ToString()));
                    lbProducts.Items[lbProducts.Items.Count - 1].Attributes.Add("title", dr["productdesc"].ToString());
                }
            }
        }
    }

    public void ClearProducts()
    {
        lbProducts.Items.Clear();
    }

    protected void ddlProductTypes_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        if (ddlProductTypes.SelectedIndex > 0)
        {
            loadProducts();
        }
        else
        {
            lbProducts.Items.Clear();
        }
    }

    protected void btnAdd_Click(object sender, System.EventArgs e)
    {
        if (lbProducts.SelectedIndex != -1)
        {
            if (lbOrderProducts.Items.Count == 0)
            {
                lblError1.Text = "";
                lblError2.Text = "";
                WriteProduct();
                loadOrders();
            }
            else
            {
                lblError1.Text = "Only a single product type allowed";
            }


        }
        ddlProductQty.SelectedValue = "1";
    }

    protected void btnRemove_Click(object sender, System.EventArgs e)
    {
        if (lbOrderProducts.SelectedIndex != -1)
        {
            ((DataAccess)Session["App"]).RemoveOrderItem(int.Parse(lbOrderProducts.SelectedValue));
        }
        loadOrders();
    }

    public void WriteProduct()
    {
        ((DataAccess)Session["App"]).PutOrderItem(int.Parse(lbProducts.SelectedValue), int.Parse(ddlProductQty.SelectedValue));
    }

    public void loadOrders()
    {
        lbOrderProducts.Items.Clear();

        DataTable dtOrder = new DataTable();
        dtOrder = ((DataAccess)Session["App"]).GetOrder();

        foreach (DataRow dr in dtOrder.Rows)
        {
            lbOrderProducts.Items.Add(new ListItem(dr["quantity"] + " - " + dr["productdesc"], dr["productid"].ToString()));
            lbOrderProducts.Items[lbOrderProducts.Items.Count - 1].Attributes.Add("title", dr["productdesc"].ToString());
        }
    }

    protected void ddlAutoship_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadProducts();
    }
}
